package com.skydroid.demo.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.skydroid.demo.R
import com.skydroid.demo.utils.String2ByteArrayUtils
import java.lang.StringBuilder
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress

/**
 * author:CQD

 * e-mail:634897993@qq.com

 * data:  2020/4/29

 * desc:
 *
 * version:
 */
class DataTransmissionActivity:AppCompatActivity() {

    private val TAG = DataTransmissionActivity::class.java.name

    private var datagramSocket : DatagramSocket? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_data_transmission)
        initView()
    }

    companion object{
        fun start(context: Context){
            context.startActivity(Intent(context,
                DataTransmissionActivity::class.java))
        }
    }

    private fun initView(){
        findViewById<View>(R.id.btn_read_13551).setOnClickListener {
            Thread{
                read13551()
            }.start()
        }
        findViewById<View>(R.id.btn_read_14551).setOnClickListener {
            Thread{
                read14551()
            }.start()
        }
        findViewById<View>(R.id.btn_connect_13552).setOnClickListener {
            Thread{
                connect13551()
            }.start()
        }
        findViewById<View>(R.id.btn_connect_14552).setOnClickListener {
            Thread{
                connect14551()
            }.start()
        }
        findViewById<View>(R.id.btn_send).setOnClickListener {
            Thread{
                send()
            }.start()
        }
    }

    private fun read13551(){
        try {
            datagramSocket =  DatagramSocket(13551)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        read()
    }

    private fun connect13551(){
        try {
            datagramSocket =  DatagramSocket(13551)
            datagramSocket?.connect(InetAddress.getByName("127.0.0.1"), 13552)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        read()
    }


    private fun read14551(){
        try {
            datagramSocket =  DatagramSocket(14551)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        read()
    }

    private fun connect14551(){
        try {
            datagramSocket =  DatagramSocket(14551)
            datagramSocket?.connect(InetAddress.getByName("127.0.0.1"), 14552)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        read()
    }

    var count = 0

    private fun read(){
        try {
            while (datagramSocket != null) {
                var cache = ByteArray(256)
                var packet = DatagramPacket(cache , cache.size)
                datagramSocket?.receive(packet)
                //packet.length 是读取到的包长度
                var tempArray = ByteArray(packet.length)

                for (index in 0 until packet.length){
                    tempArray[index] = packet.data[index]
                }
                count += packet.length
                Log.e(TAG,"收到：${String2ByteArrayUtils.bytes2Hex(tempArray)} ++++ count = ${count}")
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    private fun send(){
        try {
            var packet = DatagramPacket( ByteArray(256), 256)
            packet.data = byteArrayOf(1,2,3,4)
            datagramSocket?.send(packet)
            Log.e(TAG,"发送：${String2ByteArrayUtils.bytes2Hex(packet.data)}")
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        datagramSocket?.close()
    }
}